"""
@name : myregex
@author : wenyao
@projectname: sanchuanglianxi
"""
#1、uninx操作系统  sed  grep

#re 模块
import re
#search 匹配发现的第一个字符串就返回
# rest = re.search("sanle", "Hello sanle sanle")
# print(rest)

#match  同search一样，但是从字符串的头开始匹配
# rest = re.match("sanle", "sanle Hello sanle")
# print(rest)

#raw原始字符  转义字符\n 表示换行  原始字符就表示'\n',不是换行
# rest = re.search(r'\\tsanle', 'Hello\\tsanle')
# print(rest)

#match对象：
# msg = "It's rainning cats and dogs cats"
# match = re.search(r'cats',msg)
# print(match)
# print(match.group())
# print(match.start())
# print(match.end())
# print(match.groups())

#findall  or finditer
# rest = re.findall(r'cats', "It's rainning cats and dogs cats")
# print(rest)
#
# rest = re.finditer(r'cats', "It's rainning cats and dogs cats")
# print(rest)

#sub 匹配替换
# msg = "I am learning python3"
# print(re.sub(r'python', 'PYTHON', msg))

#complie
# msg = "I am learning python3"
# reg = re.compile(r'python')
# print(reg.findall(msg))
#
# msg2 = "I am learning python2"
# print(reg.findall(msg2))

#基本的正则匹配
#区间
# ret = re.findall(r'Python','Python 3 python')
# print(ret)
# ret = re.findall(r'[Pp]ython', 'Python 3 python')
# print(ret)
# #匹配所有字母 [a-zA-Z]
# ret = re.findall(r'[a-z]','abcABC123')
# print(ret)
# #匹配所有的字母及数字
# ret = re.findall(r'[.]',"aBc123\#.%")
# print(ret)

#或匹配
# msg = "It's rainning cats and dogs"
# ret = re.search(r'(cat|dog)',msg).group()
# print(ret)
# ret = re.findall(r'(cat|dog)',msg)
# print(ret)

#取反
# ret = re.search(r'a[^a-z]','1ab3cb3a1')
# print(ret.group())

#开始与结束 ^ , $
# ret = re.findall(r'^python','python3#')
# print(ret)
# ret = re.findall(r'python$','python3#')
# print(ret)

#任意一个字符
# ret = re.findall(r'p.thon','pYthon python pthon p-thon')
# print(ret)

#快捷方式
# ret = re.findall(r'\w','python3#')
# print(ret)
#
# ret = re.findall(r'\W','python3#')
# print(ret)

# msg = "hello world, this is 4"
# ret = re.findall(r'\d', msg)
# print(ret)

#\b单词边界
# msg = "def world abchello"
#
# ret = re.findall(r'\bdef\b', msg)
# print(ret)

# ret = re.findall(r'\d?','python33#')
# print(ret)

# ret = re.findall(r'\w*', 'python3#')
# print(ret)

# ret = re.findall(r'\w{1,4}', 'python3##abc#x')
# print(ret)

# msg = "<div>test1</div>bb<div>test2</div>"
# ret = re.findall("<div>.*</div>", msg)
# print(ret)
# ret = re.findall("<div>.*?</div>", msg)
# print(ret)


#
#print(re.findall("abc*","abccccabc"))

#分组
# ret = re.search(r'([\d]{3})-([\d]{3})-([\d]{3})','123-321-234abc')
# print(ret.groups())
# print(ret.group())
# print(ret.group(0))
# print(ret.group(1))
# print(ret.group(2))
# print(ret.group(3))
# #print(ret.group(4))
# print(ret.expand("this num is \\1"))

#分组向后引用
#捕获分组
# d = "a1 a2 a1 a2"
# print(re.search(r"(\w\d)\s+(\w\d)\s+\1\s+\2",d).group())
#非捕获分组
# d = "a1 a2 a2 a1"
# ret = re.search(r"(?:\w\d)\s+(\w\d)\s+\1\s",d)
# print(ret.group())
# print(ret.group(1))
#print(ret.group(2))

#命名分组
# ret = re.search(r'(?P<name>[\d]{3})-[\d]{3}-(?P<last>[\d]{3})',"123-321-234abc")
# print(ret.group())
# print(ret.group(1))
# print(ret.group('first'))
# # print(ret.groupdict())
# # print(ret.groupdict().get('first',""))

# msg = """
# <div>hello</div>
# <h1>hello</h1>
# """
# ret = re.findall(r'(<(\w+)>hello</\2>)',msg)
# print(ret)
# ret = re.search(r'(<(\w+)>hello</\2>)',msg)
# print(ret.groups())

#findall  有捕获分组，只输出捕获分组里的内容
# msg = "123@qq.comaaa@163.combbb@126.comxx@321.comabc@abc.com"
# pattern = re.compile("(?:\w+)\@(?:126|163|qq)\.com")
# print(pattern.findall(msg))

# msg = 'san chuang tong le, huan ying "hello world nihao wen" , learning python'
# ret = re.findall(r'\w+\b|(?:")[\w ]+(?:")',msg)
# print(ret)

#正则表达式 标记位
# Python
# python
#^  $
msg = '''
Python
python
'''
# ret = re.findall('^python$','Python',re.I)
# print(ret)
# ret = re.findall('^python$',msg,re.I)
# print(ret)
# ret = re.findall('^python$',msg,re.M|re.I)
# print(ret)

# ret = re.findall('.+',msg)
# print(ret)
# ret = re.findall('.+',msg, re.S)
# print(ret)

# ret = re.findall("""^python #匹配python开头
#                    $  #匹配python结尾""" ,msg, re.X|re.M|re.I)
# print(ret)

# msg = "http://www.baidu.com"
# #msg = "http://baidu.com/"
# #msg = "https://www.baidu.com/" #--->www.baidu.com
# #msg = "absdfwdsferqwer"
# ret = re.findall(r'\w+[\w.]+\.\w+',msg)
# ret = re.findall(r'[\w.]+\.com\b', msg)
# ret = re.findall(r'https?://((?:[\w-]+\.)+[\w-]+)/?', msg)
# print(ret)

# msg = "+1217375722991"
# ret = re.findall(r'^(?:\+\d{2,3})?(1\d{10})$',msg)
# print(ret)

#零宽断言
# s = "a regular rbgular  expression"
# print(re.findall(r"re(?=gular)",s))
# print()
# print(re.findall(r"re(?!gular)",s))
#print(re.findall(r'(?<=re)gular',s))
# print(re.findall(r'(?<!re)gular',s))

# s = 'inet 192.168.0.66/24 brd 192.168.0.255 scope global dynamic noprefixroute ens33'
# print(re.findall(r'(?<=inet )(.*?)(?=/)',s))
#print(re.findall(r'\w',"ab_12"))
#print(re.findall(r'^(?:\w+){6,18}@[a-zA-Z0-9-]+\.com$',"sanchuangtongle@edu.sc.com"))
#ip 地址 怎么去匹配  判断合法性

# import re
# str=input("请输入你的电子邮箱：")
# u=re.findall(r'^(?:\w+){6,18}@[a-zA-Z0-9-]+.com$',str)
# if u!=[]:
#     print(u)
# else:
#     print("电子邮箱错误！")
